package com.michael.study.jetpack.module.dispatcher

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.michael.study.jetpack.R
import com.michael.study.jetpack.databinding.ActivityDispatcherBinding
import java.lang.Exception

class DispatcherActivity : AppCompatActivity(), View.OnClickListener {

    private lateinit var mBinding: ActivityDispatcherBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = ActivityDispatcherBinding.inflate(layoutInflater)
        setContentView(mBinding.root)
        var action = intent.action
        if (action != null && action.equals(Intent.ACTION_VIEW)) {
            val data = intent.data
            Log.d("lgq", "onCreate: " + data.toString())
        }
        mBinding.btnGoApp.setOnClickListener(this)
        mBinding.btnGoPad.setOnClickListener(this)
        mBinding.btnGoAccessory.setOnClickListener(this)
        mBinding.btnGoOrder.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.btn_go_app -> {
                try {
                    val url = "autel://com.autel.cloud.maxifix/home?url=2222&type=1"
                    //val url = "lgq://com.michael.study.viewpager/home"
                    val intent = Intent(
                        Intent.ACTION_VIEW,
                        Uri.parse(url)
                    )
                    startActivity(intent)
                } catch (e: Exception) {
                    showToast("跳转失败")
                }

            }
            R.id.btn_go_pad -> {
                try {
                    val url = "autel://com.autel.cloud.maxifixhd/home?url=2222&type=1"
                    val intent = Intent(
                        Intent.ACTION_VIEW,
                        Uri.parse(url)
                    )
                    startActivity(intent)
                } catch (e: Exception) {
                    showToast("跳转失败")
                }
            }

            R.id.btn_go_accessory -> {
                try {
                    val url = "autel://com.autel.cloud.maxifixhd/home?url=/pages/remote-assist-module/views/tech-createOrder/createorder?type=1"
                    val intent = Intent(
                        Intent.ACTION_VIEW,
                        Uri.parse(url)
                    )
                    startActivity(intent)
                } catch (e: Exception) {
                    showToast("跳转失败")
                }
            }

            R.id.btn_go_order -> {
                try {
                    val url = "autel://com.autel.cloud.maxifixhd/home?url=/pages/sms-module/views/inquiry/search/index?vin=abc&keyword=123 "
                    val intent = Intent(
                        Intent.ACTION_VIEW,
                        Uri.parse(url)
                    )
                    startActivity(intent)
                } catch (e: Exception) {
                    showToast("跳转失败")
                }
            }
        }
    }

    fun showToast(msg: String) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
    }
}